package com.wxproject.demo.Controller.Article;

import com.wxproject.demo.Common.lang.ResultBody;

import com.wxproject.demo.Entity.AO.CommentReplyAO;
import com.wxproject.demo.Entity.AO.ReplyArticleAO;
import com.wxproject.demo.Entity.AO.ReplyTwoCommentAO;
import com.wxproject.demo.Service.CommentService;
import com.wxproject.demo.Utils.JWTUtil;
import com.wxproject.demo.Utils.SnowFlake;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;

/**
 * @author zqf
 * @date 2021/4/30 18:49
 * @description
 */
@RestController
//@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;

    //回复文章
    @PostMapping("/replyArticle")
    @RequiresRoles(logical = Logical.OR, value = {"user"})
    public ResultBody replyArticle(@Validated @RequestBody ReplyArticleAO articleAO,HttpServletRequest rq){
        String userId = JWTUtil.getUserId(rq.getHeader("Token"));
        articleAO.setUserId(userId);
        articleAO.setId(String.valueOf(SnowFlake.nextId()));
        if (commentService.replyArticle(articleAO)!=0) {
            return ResultBody.ok().message("发布成功");
        }
        return ResultBody.error().message("发布失败");
    }

    //回复一级评论的二级评论
    @PostMapping("/CommentReply")
    @RequiresRoles(logical = Logical.OR, value = {"user"})
    public ResultBody CommentReply(@Validated @RequestBody CommentReplyAO comment, HttpServletRequest rq){
        String userId = JWTUtil.getUserId(rq.getHeader("Token"));
        comment.setUserId(userId);
        comment.setId(String.valueOf(SnowFlake.nextId()));
        if (commentService.commentReply(comment)!=0) {
            return ResultBody.ok().message("发布成功");
        }
        return ResultBody.error().message("发布失败");
    }

    //回复二级评论
    @PostMapping("ReplyTwoComment")
    @RequiresRoles(logical = Logical.OR, value = {"user"})
    public ResultBody ReplyTwoComment(@Validated @RequestBody ReplyTwoCommentAO twoCommentAO, HttpServletRequest rq){
        String userId = JWTUtil.getUserId(rq.getHeader("Token"));
        twoCommentAO.setFromUid(userId);
        twoCommentAO.setId(String.valueOf(SnowFlake.nextId()));
        if (commentService.replyTwoComment(twoCommentAO)!=0) {
            return ResultBody.ok().message("发布成功");
        }
        return ResultBody.error().message("发布失败");
    }
}
